package Thread;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyFixThreadPool{ 
    //定义一个阻塞队列
    private BlockingQueue<Runnable> queue =new LinkedBlockingQueue<>();
    //定义构造方法，创建线程池，n为线程池的长度
    public MyFixThreadPool(int n){
        //创建n个线程，执行任务
        for(int i=0;i<n;i++){ 
            //创建线程
            Thread t=new Thread(()->{
                try{
                    //重阻塞队列取出任务执行
                    while(true){
                        Runnable task=queue.take();
                        task.run();
                    }
                }catch(InterruptedException e){
                    e.printStackTrace();
                }

            });
            //启动线程
            t.start();
        }
    }
    //定义submit()方法，提交任务
    public void submit(Runnable task) throws InterruptedException {
        //往阻塞队列中放入任务
        queue.put(task);
    }
}

public class Demo4 {
    public static void main(String[] args) throws InterruptedException {
        MyFixThreadPool pool=new MyFixThreadPool(10);
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello world");
            }
        });
    }
}   
